package info.sean.main;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
  /**
   *获取每月最后一天时间
   * @param sDate1
   * @return
   */
  public static Date getLastDayOfMonth(Date sDate1)  {
    Calendar cDay1 = Calendar.getInstance();
    cDay1.setTime(sDate1);
    final int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
    Date lastDate = cDay1.getTime();
    lastDate.setDate(lastDay);
    return lastDate;
  }

  /*
  获取下一个月第一天凌晨1点
   */
  public static Date nextMonthFirstDate() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 1);  //设置为每月凌晨1点
    calendar.set(Calendar.DAY_OF_MONTH, 1);  //设置为每月1号
    calendar.add(Calendar.MONTH, 1);  // 月份加一，得到下个月的一号
//    calendar.add(Calendar.DATE, -1);   下一个月减一为本月最后一天
    return calendar.getTime();
  }

  /**
   * 获取第二天凌晨0点毫秒数
   * @return
   */
  public static Date nextDayFirstDate() throws ParseException {
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.add(Calendar.DAY_OF_YEAR, 1);
    cal.set(Calendar.HOUR_OF_DAY, 00);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    return cal.getTime();
  }

  //*********

  /**
   * 获取当前时间到下个月凌晨1点相差秒数
   * @return
   */
  public static Long getSecsToEndOfCurrentMonth(){

    Long secsOfNextMonth = nextMonthFirstDate().getTime();
    //将当前时间转为毫秒数
    Long secsOfCurrentTime = new Date().getTime();
    //将时间转为秒数
    Long distance = (secsOfNextMonth - secsOfCurrentTime)/1000;
    if (distance > 0 && distance != null){
      return distance;
    }
    return new Long(0);
  }

  /**
   * 获取当前时间到明天凌晨0点相差秒数
   * @return
   */
  public static Long getSecsToEndOfCurrentDay() throws ParseException {

    Long secsOfNextDay = nextDayFirstDate().getTime();
    //将当前时间转为毫秒数
    Long secsOfCurrentTime = new Date().getTime();
    //将时间转为秒数
    Long distance = (secsOfNextDay - secsOfCurrentTime)/1000;
    if (distance > 0 && distance != null){
      return distance;
    }
    return new Long(0);
  }

  public static void main(String[] args) throws ParseException {
    System.out.println("getSecsToEndOfCurrentDay() = " + getSecsToEndOfCurrentDay()/3600);
    System.out.println("nextDayFirstDate() = " + nextDayFirstDate());

    Long secsOfNextDay = nextDayFirstDate().getTime();
    System.out.println("secsOfNextDay = " + secsOfNextDay);


  }
}
